//package com.matt.service.impl;
//
//import com.matt.domain.*;
//import com.matt.foundation.constant.ContractConstant;
//import com.matt.commons.dto.sso.StaffDTO;
//import com.matt.foundation.utils.DateUtil;
//import com.matt.foundation.vo.ImportDataVo;
//import com.matt.repository.*;
//import com.matt.repository.base.ClienteleNativeRepository;
//import com.matt.service.ImportService;
//import com.matt.remoteCall.SsoCallService;
//import org.apache.commons.lang.StringUtils;
//import org.springframework.beans.BeanUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.math.BigDecimal;
//import java.math.RoundingMode;
//import java.util.Date;
//import java.util.List;
//import java.util.Objects;
//
///**
// * *****************
// * Created by Ron
// * from com.matt
// * on 2018/7/27 0027.
// * *******************
// */
//@Service
//public class ImportServiceImpl implements ImportService {
//
//    @Autowired
//    private LeadsRepository leadsRepository;
//    @Autowired
//    private ClienteleRepository clienteleRepository;
//    @Autowired
//    private ContractRepository contractRepository;
//    @Autowired
//    private LeadsHistoryRepository leadsHistoryRepository;
//    @Autowired
//    private ContractHistoryRepository contractHistoryRepository;
//    @Autowired
//    private ProductRepository productRepository;
//    @Autowired
//    private ContractProductRepository contractProductRepository;
//    @Autowired
//    private SsoCallService ssoCallService;
//
//    @Autowired
//    private ClienteleNativeRepository clienteleNativeRepository;
//
//
//    @Transactional
//    @Override
//    public String migrateData(ImportDataVo vo) {
//
//        //基本数据查询
//        StaffDTO staffDTO = ssoCallService.getStaffDTO(null, vo.getContract_staffId());
//
//        String staffId = staffDTO.getId();
//        String staffName = staffDTO.getRealName();
//        String areaId = vo.getAreaId();
//        String headImgUrl = staffDTO.getHeadImgUrl();
//
//        Leads leads = leadsRepository.findOne(vo.getClientele_id());
//        if (Objects.isNull(leads)) {
//            leads = new Leads();
//            leads.setCreated(DateUtil.parseToDateMilis(vo.getClientele_createDate()));
//            //leads.setChannelStatus(8);
//            leads.setChannel("系统导入");
//            leads.setCategory(1); //线索方式==主动
//            leads.setStatus(4); //线索状态==已到访
//            leads.setChangeStatus(1); //已转
//            leads.setChangeStatusVal("已转");
//            leads.setIsComplete("是");
//            leads.setChildName(vo.getClientele_childName());
//            leads.setBirthday(DateUtil.parseToDateMilis(vo.getClientele_childBirthday()));
//            leads.setNickName(vo.getClientele_nickName());
//            leads.setGender(vo.getClientele_gender());
//            leads.setId(vo.getClientele_id());
//            leads.setName(vo.getClientele_name());
//            leads.setRelationship(vo.getClientele_relationship());
//            leads.setMobile(vo.getClientele_phone());
//            leads.setHomeAddress(vo.getClientele_homeAddress());
//            leads.setCompany(vo.getClientele_company());
//            leads.setAreaId(areaId);
//            leads.setStaffId(staffId);
//            leadsRepository.save(leads);
//
//            //1.1 保存线索操作日志记录信息
//            LeadsHistory leadsHistory = new LeadsHistory();
//            leadsHistory.setType("leads");
//            leadsHistory.setContent("系统导入");
//            leadsHistory.setActualFollowDate(new Date());
//            leadsHistory.setLeadsId(leads.getId());
//            leadsHistory.setAreaId(areaId);
//            //待完善
//            leadsHistory.setStaffId(staffId);
//            leadsHistory.setStaffHeadImgUrl(headImgUrl);
//            leadsHistory.setStaffName(staffName);
//            leadsHistoryRepository.save(leadsHistory);
//            //添加主动例子统计数据
////            analysisService.create(leads.getId(), leads.getCategory(), leads.getChannelStatus(), leads.getAreaId(), leads.getStaffId(), leads.getCreated());
//        }
//        /**************************/
//        Leads leadsVo = new Leads();
//        BeanUtils.copyProperties(leads, leadsVo);
//
//        Clientele clientele = clienteleRepository.findOne(vo.getClientele_id());
//
////        Clientele clientele = null;
//        if (Objects.isNull(clientele)){
//
//            //2. 保存客户信息
//            clientele = new Clientele();
//            //BeanUtils.copyProperties(leadsVo,clientele);
//            clientele.setId(vo.getClientele_id());
//            clientele.setChannelId(leadsVo.getChannelId());
//            clientele.setChannel(leadsVo.getChannel());
//            clientele.setCategory(leadsVo.getCategory());
//            clientele.setBirthday(leadsVo.getBirthday());
//            clientele.setGender(vo.getClientele_gender());
//            clientele.setName(vo.getClientele_name());
//            clientele.setClienteleType("A");
//            clientele.setRelationship(vo.getClientele_relationship());
//            clientele.setMobile(vo.getClientele_phone());
//            clientele.setHomeAddress(vo.getClientele_homeAddress());
//            clientele.setCompany(vo.getClientele_company());
//
//            clientele.setCreated(DateUtil.parseToDateMilis(vo.getClientele_createDate()));
//            clientele.setStatus(6); // 6=已交定金
//            clientele.setStatusVal("已交定金");
//
//            List<Clientele> clienteleList = clienteleRepository.findByChildNameAndAreaId(vo.getClientele_childName(), areaId);
//            if (clienteleList.size() > 0){
//                String adviseName = getAdviseName(vo.getAreaId(), vo.getClientele_childName());
//                clientele.setAdviceName(adviseName);
//                clientele.setSysChildName(adviseName);
//            }else{
//                clientele.setAdviceName(vo.getClientele_childName());
//                clientele.setSysChildName(vo.getClientele_childName());
//            }
//
//            clientele.setChildName(vo.getClientele_childName());
//            clientele.setNickName(vo.getClientele_nickName());
//            clientele.setClienteleType("A"); //A=A类：有支付能力也有消费意愿
//
//            //待完善
//            clientele.setStaffId(staffId);
//            clientele.setStaffName(staffName);
//            clientele.setAreaId(areaId);
//
//            clientele.setLeadsId(leadsVo.getId());
//            clienteleRepository.save(clientele);
//            Clientele clienteleVo = new Clientele();
//
//            BeanUtils.copyProperties(clientele, clienteleVo);
//
//            //2.1 保存客户操作日志记录信息
//            LeadsHistory leadsHistoryClien = new LeadsHistory();
//            leadsHistoryClien.setType("clientele");
//            leadsHistoryClien.setContent("系统导入");
//            leadsHistoryClien.setActualFollowDate(new Date());
//            leadsHistoryClien.setLeadsId(leads.getId());
//            leadsHistoryClien.setClientelesId(clientele.getId());
//            leadsHistoryClien.setAreaId(areaId);
//
//            leadsHistoryRepository.save(leadsHistoryClien);
//        }
//
//        Contract contract = contractRepository.findOne(vo.getContract_id());
//        if (Objects.isNull(contract)){
//            Product product = productRepository.findOne(vo.getContract_product_productId());
//            BigDecimal contractTotalAmount = BigDecimal.ZERO;
//            if (vo.getContract_totalAmount() == null)
//                contractTotalAmount = product.getStandardAmount().multiply(BigDecimal.valueOf(vo.getContract_product_count()));
//            else
//                contractTotalAmount = BigDecimal.valueOf(vo.getContract_totalAmount());
//
//            /**************************/
//            //3. 保存合同信息
//            contract = new Contract();
//            contract.setId(vo.getContract_id());
//            contract.setName(vo.getContract_name());
//            contract.setCode(vo.getContract_code());
//            contract.setBeginDate(DateUtil.parseToDateMilis(vo.getContract_beginDate()));
//
//            if (vo.getContract_status() == 0){
//                contract.setStatus(ContractConstant.Status.NON_START.codeOf());
//                contract.setStatusVal(ContractConstant.Status.NON_START.valueOf());
//            }
//            if (vo.getContract_status() == 1){
//                contract.setStatus(ContractConstant.Status.NORMAL.codeOf());
//                contract.setStatusVal(ContractConstant.Status.NORMAL.valueOf());
//            }
//            if (vo.getContract_status() == 2){
//                contract.setStatus(ContractConstant.Status.STOP.codeOf());
//                contract.setStatusVal(ContractConstant.Status.STOP.valueOf());
//            }
//            if (vo.getContract_status() == 3){
//                contract.setStatus(ContractConstant.Status.END.codeOf());
//                contract.setStatusVal(ContractConstant.Status.END.valueOf());
//            }
//
////            contract.setPayStatus(1);
////            contract.setPayStatusVal("已付清");
//            contract.setEndDate(DateUtil.parseToDateMilis(vo.getContract_endDate()));
//            contract.setSignDate(DateUtil.parseToDateMilis(vo.getContract_createDate()));
////            contract.setPayType(2);
////            contract.setPayTypeVal("全款");
//            contract.setPayChannel(1);
//            contract.setPayChannelVal("现金");
//            contract.setTotalAmount(contractTotalAmount);
////            contract.setPracticalAmount(contractTotalAmount);
//            contract.setClienteleId(clientele.getId());
//            contract.setType(vo.getContract_type());
//            contract.setLastModified(DateUtil.parseToDateMilis(vo.getContract_createDate()));
//            contract.setSignDate(DateUtil.parseToDateMilis(vo.getContract_createDate()));
//            contract.setPaidDate(DateUtil.parseToDateMilis(vo.getContract_createDate()));
//            contract.setStaffId(staffId);
//            contract.setRemark(vo.getContract_remark());
//            contract.setFromChannel(0);
//            contract.setCreated(DateUtil.parseToDateMilis(vo.getContract_createDate()));
//            contract.setIsRenewal(contractRepository.findByClienteleId(contract.getClienteleId()).size() > 0); //是否是续约合同 0:不是，1:是
//
//
//            //添加例子成交统计数据
////        analysisService.deal(leads.getId(), leads.getCategory(), leads.getChannelStatus(), leads.getAreaId(), staffId, leads.getCreated());
//
//            //待完善
//            contract.setAreaId(areaId);
//            contract.setStaffName(staffName);
//            contractRepository.save(contract);
//
//            Contract contractVo = new Contract();
//            BeanUtils.copyProperties(contract, contractVo);
//
//            //3.1 保存合同操作日志记录信息
//            ContractHistory contractHistory = new ContractHistory();
//            contractHistory.setCreated(DateUtil.parseToDateMilis(vo.getClientele_createDate()));
//            contractHistory.setContent("系统导入");
//            contractHistory.setClienteleId(clientele.getId());
//            contractHistory.setStaffId(vo.getContract_staffId());
//            contractHistory.setContractId(contractVo.getId());
//
//            //待完善
//            contractHistory.setStaffName(staffName);
//            contractHistory.setStaffHeadImgUrl(headImgUrl);
//
//            contractHistoryRepository.save(contractHistory);
//
//            /**************************/
//
//            //4. 保存合同购买产品信息
//
//
//            ContractProduct contractProduct = new ContractProduct();
//
//            contractProduct.setProductId(vo.getContract_product_productId());
//            contractProduct.setContractId(contractVo.getId());
//            contractProduct.setClienteleId(clientele.getId());
//            contractProduct.setProductDetailType(product.getProductDetailType());
//            contractProduct.setType(2); //类型   1:意向（转客户） 2：已购买（签合同）
//            contractProduct.setTypeVal("已购买");
//            contractProduct.setCount(vo.getContract_product_count());
//            contractProduct.setRemainCount(new Double(0));
//
//            contractProduct.setPracticalAmount(contractTotalAmount.divide(BigDecimal.valueOf(vo.getContract_product_count()), 2, RoundingMode.HALF_UP));//实际单价 保留小数点后两位
//            contractProduct.setPracticalTotalAmount(contractTotalAmount);
//            contractProduct.setStandardAmount(product.getStandardAmount());
//            contractProduct.setStandardTotalAmount(product.getStandardAmount().multiply(BigDecimal.valueOf(vo.getContract_product_count())));
//            contractProduct.setRemark("系统导入数据");
//            contractProductRepository.save(contractProduct);
//        }
//
//
//        return clientele.getId();
//    }
//    private String getAdviseName(String areaId, String childName){
//        //当成真是名字查询
//        List<Clientele> list = clienteleNativeRepository.findChilds(areaId, childName);
//        //当成系统名查询
//        String adviceName = "";
//        if (list.size() > 0) {
//            adviceName = list.get(0).getAdviceName() + "_A01";
//        } else if (list.size() > 1) {
//            adviceName = StringUtils.substringBefore(list.get(0).getAdviceName(), "A") + "A" + String.format("%02d", Integer.parseInt(StringUtils.substringAfter(list.get(0).getAdviceName(), "A")) + 1);
//        }
//        return adviceName;
//    }
//}
